<html>
<head>    
	<script type="text/javascript">
		var aJaxURL			= "server-side/view/partner.action.php";			//server side folder url
		var a_aJaxURL		= "server-side/view/partner/accounts.action.php";
		var b_aJaxURL		= "server-side/view/partner/bank.action.php";
		var c_aJaxURL		= "server-side/view/partner/cadre.action.php";
		
		var tName			= "example";												//table name
		var fName			= "add-edit-form";											//form name
		
		$(document).ready(function () {
			LoadTable(tName);
			
			/* Add Button ID, Delete Button ID */
            GetButtons("add_button", "delete_button", "", "");
			
			SetEvents("add_button", "delete_button", "check-all", tName, fName, aJaxURL);
		});
        
		function LoadTable(table, par_id){
			if(!par_id){
				par_id	= $("#part_id").val();
			}
			
			switch (table) {
				case "acc_details":
				  	/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
					GetDataTable(table, a_aJaxURL, "get_list", 5, "part_id=" + par_id, 0, "", 1, "desc");
					$("#" + table).width("100%");
			    break;
				case "bank_details":
				  	/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
					GetDataTable(table, b_aJaxURL, "get_list", 5, "part_id=" + par_id, 0, "", 0, "desc");
					$("#" + table).width("100%");
			    break;
				case "cadre_details":
				  	/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
					GetDataTable(table, c_aJaxURL, "get_list", 5, "part_id=" + par_id, 0, "", 0, "desc");
					$("#" + table).width("100%");
			    break;
				default:
				    /* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
				    GetDataTable(table, aJaxURL, 'get_list', 7, "", 0, "", 1, "asc");
			}
		}
		
		function LoadDialog(form){
			switch (form) {
				case "add-edit-acc-form":
					var id		= $("#bank_id").val();
					if(id != ""){
						$("#b_name").attr("disabled", "disabled");
					}
					
					var accButt = {
				        "save": {
				            text: "შენახვა",
				            id: "save-acc-dialog",
				            click: function () {
				            }
				        },
				        "cancel": {
				            text: "დახურვა",
				            id: "cancel-dialog",
				            click: function () {
				                $(this).dialog("close");
				            }
				        }
				    };
					
					/* Dialog Form Selector Name, Buttons Array */
					GetDialog(form, 440, "auto", accButt);
					break;
				case "add-edit-bank-form":
					var id		= $("#bank_id").val();
					if(id != ""){
						$("#b_name").attr("disabled", "disabled");
					}
					
					var bankButt = {
				        "save": {
				            text: "შენახვა",
				            id: "save-bank-dialog",
				            click: function () {
				            }
				        },
				        "cancel": {
				            text: "დახურვა",
				            id: "cancel-dialog",
				            click: function () {
				                $(this).dialog("close");
				            }
				        }
				    };
					
					/* Dialog Form Selector Name, Buttons Array */
					GetDialog(form, 400, "auto", bankButt);
					break;
				case "add-edit-cadre-form":
					var id		= $("#cadre_id").val();
					if(id != ""){
						$("#c_fname").attr("disabled", "disabled");
						$("#c_lname").attr("disabled", "disabled");
					}
					
					var cadreButt = {
				        "save": {
				            text: "შენახვა",
				            id: "save-cadre-dialog",
				            click: function () {
				            }
				        },
				        "cancel": {
				            text: "დახურვა",
				            id: "cancel-dialog",
				            click: function () {
				                $(this).dialog("close");
				            }
				        }
				    };
					
					/* Dialog Form Selector Name, Buttons Array */
					GetDialog(form, 440, "auto", cadreButt);
					break;
				default:
					/* Dialog Form Selector Name, Buttons Array */
					GetDialog(fName, 900, "auto", "");
					
					LoadTabs();
					
					var id		= $("#part_id").val();
					GetDate("p_data");
					
					if(id == ""){
						$("#tabs").tabs({disabled: [1,2,3]});
					}else{						
						$("#p_user_id").attr("disabled", "disabled");
						
						LoadTable("acc_details", id);
						GetButtons("add_acc_button", "", "export_acc_button", "");
						SetEvents("add_acc_button", "delete_acc_button", "check-all-acc", "acc_details", "add-edit-acc-form", a_aJaxURL);
						
						LoadTable("bank_details", id);
						GetButtons("add_bank_button", "delete_bank_button", "export_bank_button", "");
						SetEvents("add_bank_button", "delete_bank_button", "check-all-bank", "bank_details", "add-edit-bank-form", b_aJaxURL);
						
						LoadTable("cadre_details", id);
						GetButtons("add_cadre_button", "delete_cadre_button", "export_cadre_button", "");
						SetEvents("add_cadre_button", "delete_cadre_button", "check-all-cadre", "cadre_details", "add-edit-cadre-form", c_aJaxURL);
					}
			}
		}
		
		function LoadTabs(){
	        GetTabs("tabs");
		}
	    
	    $(document).on("click", "#save-dialog", function () {
            param = new Object();

            //Action
            param.act	= "save_part";
            param.id	= $("#part_id").val();
	    	
	    	param.pui	= $("#p_user_id").val();
	    	param.pn	= $("#p_name").val();
	    	param.pua	= $("#pu_address").val();
	    	param.ppa	= $("#pp_address").val();
	    	param.pd	= $("#p_data").val();
	    	param.psn	= $("#ps_number").val();
	    	
	    	param.py	= $("#payer").val();
	    	param.ps	= $("#p_status").val();
	    	param.cf	= $("#c_form").val();
			
			var allow = true;
			if(param.py == 0){
				if(!confirm("დარწმუნებული ხართ რომ პარტნიორი არ არის დღგ-ს გადამხდელი?")) {						
					allow = false;
				}
			}
			
			if(allow){
				if(param.pui == ""){
					alert("შეავსეთ საიდ. კოდი/ პირადი №!");
				}else if(param.pn == ""){
					alert("შეავსეთ დასახელება!");
				}else if(param.pua == ""){
					alert("შეავსეთ იურიდიული მისამართი!");
				}else {
				    $.ajax({
				        url: aJaxURL,
					    data: param,
				        success: function(data) {
					        if(typeof(data.error) != "undefined"){
								if(data.error != ""){
									alert(data.error);
								}else{
									LoadTable(tName);
					        		CloseDialog(fName);
								}
							}
					    }
				    });
				}
			}
		});
		
		// Add - Save Bank
	    $(document).on("click", "#save-bank-dialog", function () {
	    	param = new Object();

            //Action
            param.act	= "save_bank";
            
	    	param.id	= $("#bank_id").val();
	    	param.p_id	= $("#part_id").val();
	    	
	    	param.bn	= $("#b_name").val();
	    	param.bb	= $("#b_branch").val();
	    	param.bc	= $("#b_code").val();
	    	param.ba	= $("#b_account").val();
						        
			if(param.bn == ""){
				alert("შეავსეთ ბანკის დასახელება!");
			}else {
			    $.ajax({
			        url: b_aJaxURL,
				    data: param,
				    success: function(data) {
			        	if(typeof(data.error) != "undefined"){
							if(data.error != ""){
								alert(data.error);
							}else{
								LoadTable("bank_details", param.p_id);
					        	CloseDialog("add-edit-bank-form");
							}
						}
				    }
			    });
			}
		});
		
		// Add - Save Cadre
	    $(document).on("click", "#save-cadre-dialog", function () {
	    	param = new Object();

            //Action
            param.act	= "save_cadre";
		    
	    	param.id	= $("#cadre_id").val();
	    	param.p_id	= $("#part_id").val();
	    	
	    	param.ui	= $("#user_id").val();
	    	param.cfn	= $("#c_fname").val();
	    	param.cln	= $("#c_lname").val();
	    	param.cp	= $("#c_position").val();
	    	param.cc	= $("#c_contact").val();
	    	param.cph	= $("#c_phone").val();
	    	param.cmp	= $("#c_m_phone").val();
	    	
	    	param.cs	= $("#c_sale").val();
	    	param.cl	= $("#c_limit").val();
						        
			if(param.cfn == ""){
				alert("შეავსეთ სახელი!");
			}else if(param.cln == ""){
				alert("შეავსეთ გვარი!");
			}else {
			    $.ajax({
			        url: c_aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined"){
							if(data.error != ""){
								alert(data.error);
							}else{
								LoadTable("cadre_details", param.p_id);
					        	CloseDialog("add-edit-cadre-form");
							}
						}
				    }
			    });
			}
		});
    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            
            <div id="dynamic">
            	<h2>პარტნიორები</h2>
            	<div id="button_area">
        			<button id="add_button">დამატება</button><button id="delete_button">წაშლა</button>
        		</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th style="width: 206px">დასახელება</th>
                            <th class="min">საიდ. კოდი</th>
                            <th class="min">ფაქტ. მისამართი</th>
                            <th class="min">ტელეფონი</th>
                            <th class="min">გვარი</th>
                            <th class="min">ანგ. ფორმა</th>
                            <th class="check">#</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_name" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_ident_code" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_address" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_phone" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_lname" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_pay_form" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                            	<input type="checkbox" name="check-all" id="check-all">
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
    
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="პარტნიორები">
    	<!-- aJax -->
	</div>
</body>
</html>